package com.sinosoft.emplmis.jobless.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sinosoft.emplmis.jobless.entity.JoblessServerEntity;
import com.sinosoft.emplmis.jobless.entity.KnryAideffectEntity;
import com.sinosoft.emplmis.jobless.entity.KnryAidrecordEntity;
import com.sinosoft.emplmis.jobless.entity.KnryTrackingrecordEntity;
import com.sinosoft.emplmis.jobless.service.KnryAideffectService;
import com.sinosoft.emplmis.jobless.service.KnryAidrecordService;
import com.sinosoft.emplmis.jobless.service.KnryTrackingrecordService;
import com.sinosoft.emplmis.jobless.web.dto.JoblessSearchData;
import com.sinosoft.emplmis.jobless.web.dto.JoblessServerData;
import com.sinosoft.emplmis.jobless.web.dto.KnryAideffectData;
import com.sinosoft.emplmis.jobless.web.dto.KnryAidrecordData;
import com.sinosoft.emplmis.jobless.web.dto.KnryTrackingrecordData;
import com.sinosoft.framework.core.common.exception.BusinessException;
import com.sinosoft.framework.core.common.utils.ResultJson;

/**
 * 困难人员服务台账管理
 * @author
 *
 */
@Controller
@RequestMapping("jobless/difficultyServer")
public class DifficultyServerController {

	ObjectMapper mapper = new ObjectMapper();
	
	/**
	 * 困难人员就业援助记录页面跳转
	 */
	@RequestMapping(value="/aidRecordMenu", method = RequestMethod.GET)
	public String aidRecord(){
		return"jobless/difficultyServer/difficultyAidRecord";
	}
	
	/**
	 * 困难人员跟踪服务记录页面跳转
	 */
	@RequestMapping(value="/trackingRecordMenu", method = RequestMethod.GET)
	public String trackingRecord(){
		return"jobless/difficultyServer/difficultyTrackingRecord";
	}
	
	/**
	 * 困难人员就业援助记录页面跳转
	 */
	@RequestMapping(value="/aidEffectMenu", method = RequestMethod.GET)
	public String aidEffect(){
		return"jobless/difficultyServer/difficultyAidEffect";
	}

	
	@Autowired
	private KnryAidrecordService knryAidrecordService;
	
	/**
	 * 根据主键unemployedId查询相关困难人员的就业援助记录
	 * @return
	 */
	@RequestMapping(value="/getAidRecordList", method = RequestMethod.POST)
	@ResponseBody
	public ResultJson getAidRecordList(JoblessSearchData data,@RequestParam("unemployedId") String unemployedId){
		ResultJson result = new ResultJson();
		String success = "";
		String message = "";	
		List<KnryAidrecordData> resultList = new ArrayList<KnryAidrecordData>();
		List<KnryAidrecordEntity> knryAidrecordEntityList = new ArrayList<KnryAidrecordEntity>();
		try {
			knryAidrecordEntityList = knryAidrecordService.selectAll(data,unemployedId);
			for (KnryAidrecordEntity knryAidrecordEntity : knryAidrecordEntityList) {
				KnryAidrecordData knryAidrecordData = new KnryAidrecordData();
				//困难人员就业援助记录实体类转化为困难人员就业援助记录dto
				knryAidrecordData.toData(knryAidrecordData,knryAidrecordEntity);
				resultList.add(knryAidrecordData);
			}
			result.setTotal(knryAidrecordService.count(unemployedId));
			result.setRows(resultList);
			success= ResultJson.SUCCESS_YES;
			message= ResultJson.MESSAGE;
		} catch (Exception e) {
			success= ResultJson.SUCCESS_NO;
			message= "获取失败";
			e.printStackTrace();
		}finally {
			result.setSuccess(success);
			result.setMessage(message);
		}
		return result;
			
	}
	
	/**
	 * 添加困难人员就业援助记录
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value="/insertAidRecord", method=RequestMethod.POST)
	@ResponseBody
	public String insertAidRecord(KnryAidrecordData data){
		KnryAidrecordEntity Entity= new KnryAidrecordEntity();
	    ResultJson resultJson = new ResultJson();
	    String result = "";
	    try {
	    	data.setId(UUID.randomUUID().toString().trim().replaceAll("-", ""));
		    Entity.toEntityData(Entity, data, "");
		    knryAidrecordService.insert(Entity);
		    resultJson.setSuccess(ResultJson.SUCCESS_YES);
		    resultJson.setMessage(ResultJson.MESSAGE);
	    } catch (BusinessException e) {
	    	resultJson.setSuccess(ResultJson.SUCCESS_NO);
		    resultJson.setMessage(e.getMessage());
		    resultJson.setResults(null);
	    } catch (Exception e1){
	    	resultJson.setSuccess(ResultJson.SUCCESS_NO);
		    resultJson.setMessage("保存失败");
		    resultJson.setResults(null);
		    e1.printStackTrace();
	    } finally{
	    	try {
	    		result = mapper.writeValueAsString(resultJson);
		    }catch (IOException e) {
		    	e.printStackTrace();
		    }
		    return result;
	    }
	}
	
	/**
	 * 删除服务信息
	 */
	@RequestMapping(value="/deleteAidRecord", method = RequestMethod.POST)
	@ResponseBody
	public String deleteAidRecord(@RequestParam("id") String id){
		String result = "";
		ResultJson resultJson = new ResultJson(); 
		try {
			knryAidrecordService.deleteByPrimaryKey(id);
			resultJson.setSuccess(ResultJson.SUCCESS_YES);
			resultJson.setMessage(ResultJson.MESSAGE);
		} catch (Exception e) {
			resultJson.setSuccess(ResultJson.SUCCESS_NO);
			resultJson.setMessage("删除失败");
			e.printStackTrace();
		}finally{
			try {
				result = mapper.writeValueAsString(resultJson);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	
	/**
	 * 回显困难人员指定就业援助台账记录
	 * @return
	 */
	@RequestMapping(value="/getAidRecordInfo", method = RequestMethod.POST)
	@ResponseBody
	public KnryAidrecordData getAidRecordInfo(@RequestParam("id") String Id){
		KnryAidrecordData knryAidrecordData = new KnryAidrecordData();
		KnryAidrecordEntity knryAidrecordEntity = knryAidrecordService.selectByPrimaryKey(Id);
		knryAidrecordData.toEntityData(knryAidrecordData, knryAidrecordEntity);
		return knryAidrecordData;
	}
	
	/**
	 * 修改保存困难人员就业援助记录服务台账
	 * @return
	 */
	@RequestMapping(value="/updateAidRecord", method = RequestMethod.POST)
	@ResponseBody
	public String updateAidRecordInfo(KnryAidrecordData record){
		String result = "";
		ResultJson resultJson = new ResultJson();
		KnryAidrecordEntity knryAidrecordEntity = new KnryAidrecordEntity();
		try {
			//将entity转化为实体类dto
			knryAidrecordEntity.toEntityData(knryAidrecordEntity, record, "");
			knryAidrecordService.updateByPrimaryKey(knryAidrecordEntity);
			resultJson.setSuccess(ResultJson.SUCCESS_YES);
			resultJson.setMessage(ResultJson.MESSAGE);
		} catch (Exception e) {
			resultJson.setSuccess(ResultJson.SUCCESS_NO);
			resultJson.setMessage("修改失败");
			e.printStackTrace();
		}finally{
			try {
				result = mapper.writeValueAsString(resultJson);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	
	@Autowired
	private KnryTrackingrecordService knryTrackingrecordService;
	
	/**
	 * 根据主键unemployedId查询相关困难人员的跟踪服务记录
	 * @return
	 */
	@RequestMapping(value="/getTrackingRecordList", method = RequestMethod.POST)
	@ResponseBody
	public ResultJson getTrackingRecordList(JoblessSearchData data,@RequestParam("unemployedId") String unemployedId){
		ResultJson result = new ResultJson();
		String success = "";
		String message = "";	
		List<KnryTrackingrecordData> resultList = new ArrayList<KnryTrackingrecordData>();
		List<KnryTrackingrecordEntity> trackingrecordEntityList = new ArrayList<KnryTrackingrecordEntity>();
		try {
			trackingrecordEntityList = knryTrackingrecordService.selectAll(data,unemployedId);
			for (KnryTrackingrecordEntity knryTrackingrecordEntity : trackingrecordEntityList) {
				KnryTrackingrecordData trackingrecordData = new KnryTrackingrecordData();
				//困难人员就业援助记录实体类转化为困难人员就业援助记录dto
				trackingrecordData.toData(trackingrecordData,knryTrackingrecordEntity);
				resultList.add(trackingrecordData);
			}
			result.setTotal(knryTrackingrecordService.count(unemployedId));
			result.setRows(resultList);
			success= ResultJson.SUCCESS_YES;
			message= ResultJson.MESSAGE;
		} catch (Exception e) {
			success= ResultJson.SUCCESS_NO;
			message= "获取失败";
			e.printStackTrace();
		}finally {
			result.setSuccess(success);
			result.setMessage(message);
		}
		return result;
			
	}
	
	/**
	 * 添加困难人员跟踪服务记录
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value="/insertTrackingRecord", method=RequestMethod.POST)
	@ResponseBody
	public String insertTrackingRecord(KnryTrackingrecordData data){
		KnryTrackingrecordEntity Entity= new KnryTrackingrecordEntity();
	    ResultJson resultJson = new ResultJson();
	    String result = "";
	    try {
	    	data.setId(UUID.randomUUID().toString().trim().replaceAll("-", ""));
		    Entity.toEntityData(Entity, data, "");
		    knryTrackingrecordService.insert(Entity);
		    resultJson.setRows(Entity);
		    resultJson.setSuccess(ResultJson.SUCCESS_YES);
		    resultJson.setMessage(ResultJson.MESSAGE);
	    } catch (BusinessException e) {
	    	resultJson.setSuccess(ResultJson.SUCCESS_NO);
		    resultJson.setMessage(e.getMessage());
		    resultJson.setResults(null);
	    } catch (Exception e1){
	    	resultJson.setSuccess(ResultJson.SUCCESS_NO);
		    resultJson.setMessage("保存失败");
		    resultJson.setResults(null);
		    e1.printStackTrace();
	    } finally{
	    	try {
	    		result = mapper.writeValueAsString(resultJson);
		    }catch (IOException e) {
		    	e.printStackTrace();
		    }
		    return result;
	    }
	}
	
	/**
	 * 删除相关困难人员的指定跟踪服务记录
	 */
	@RequestMapping(value="/deleteTrackingRecord", method = RequestMethod.POST)
	@ResponseBody
	public String deleteTrackingRecord(@RequestParam("id") String id){
		String result = "";
		ResultJson resultJson = new ResultJson(); 
		try {
			knryTrackingrecordService.deleteByPrimaryKey(id);
			resultJson.setSuccess(ResultJson.SUCCESS_YES);
			resultJson.setMessage(ResultJson.MESSAGE);
		} catch (Exception e) {
			resultJson.setSuccess(ResultJson.SUCCESS_NO);
			resultJson.setMessage("删除失败");
			e.printStackTrace();
		}finally{
			try {
				result = mapper.writeValueAsString(resultJson);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	
	/**
	 * 回显困难人员指定就业援助台账记录
	 * @return
	 */
	@RequestMapping(value="/getTrackingRecordInfo", method = RequestMethod.POST)
	@ResponseBody
	public KnryTrackingrecordData getTrackingRecordInfo(@RequestParam("id") String Id){
		KnryTrackingrecordData knryTrackingrecordData = new KnryTrackingrecordData();
		KnryTrackingrecordEntity knryTrackingrecordEntity = knryTrackingrecordService.selectByPrimaryKey(Id);
		knryTrackingrecordData.toData(knryTrackingrecordData, knryTrackingrecordEntity);
		return knryTrackingrecordData;
	}
	
	/**
	 * 修改保存失业人员服务台账
	 * @return
	 */
	@RequestMapping(value="/updateTrackingRecord", method = RequestMethod.POST)
	@ResponseBody
	public String updateTrackingRecord(KnryTrackingrecordData record){
		String result = "";
		ResultJson resultJson = new ResultJson();
		KnryTrackingrecordEntity knryTrackingrecordEntity = new KnryTrackingrecordEntity();
		try {
			//将entity转化为实体类dto
			knryTrackingrecordEntity.toEntityData(knryTrackingrecordEntity, record, "");
			knryTrackingrecordService.updateByPrimaryKey(knryTrackingrecordEntity);
			resultJson.setSuccess(ResultJson.SUCCESS_YES);
			resultJson.setMessage(ResultJson.MESSAGE);
		} catch (Exception e) {
			resultJson.setSuccess(ResultJson.SUCCESS_NO);
			resultJson.setMessage("修改失败");
			e.printStackTrace();
		}finally{
			try {
				result = mapper.writeValueAsString(resultJson);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	
	
	@Autowired
	private KnryAideffectService knryAideffectService;
	
	/**
	 * 根据主键unemployedId查询相关困难人员的就业援助效果
	 * @return
	 */
	@RequestMapping(value="/getAidEffectGridList", method = RequestMethod.POST)
	@ResponseBody
	public ResultJson getAidEffectGridList(JoblessSearchData data,@RequestParam("unemployedId") String unemployedId){
		ResultJson result = new ResultJson();
		String success = "";
		String message = "";	
		List<KnryAideffectData> resultList = new ArrayList<KnryAideffectData>();
		List<KnryAideffectEntity> knryAideffectEntityList = new ArrayList<KnryAideffectEntity>();
		try {
			knryAideffectEntityList = knryAideffectService.selectAll(data,unemployedId);
			for (KnryAideffectEntity knryAideffectEntity : knryAideffectEntityList) {
				KnryAideffectData knryAideffectData = new KnryAideffectData();
				//困难人员就业援助效果实体类转化为困难人员就业援助效果dto
				knryAideffectData.toData(knryAideffectData,knryAideffectEntity);
				resultList.add(knryAideffectData);
			}
			result.setTotal(knryAideffectService.count(unemployedId));
			result.setRows(resultList);
			success= ResultJson.SUCCESS_YES;
			message= ResultJson.MESSAGE;
		} catch (Exception e) {
			success= ResultJson.SUCCESS_NO;
			message= "获取失败";
			e.printStackTrace();
		}finally {
			result.setSuccess(success);
			result.setMessage(message);
		}
		return result;
			
	}
	
	/**
	 * 添加困难人员就业援助效果记录
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value="/insertAidEffect", method=RequestMethod.POST)
	@ResponseBody
	public String insertAidEffect(KnryAideffectData data){
		KnryAideffectEntity Entity= new KnryAideffectEntity();
	    ResultJson resultJson = new ResultJson();
	    String result = "";
	    try {
	    	data.setId(UUID.randomUUID().toString().trim().replaceAll("-", ""));
		    Entity.toEntityData(Entity, data, "");
		    knryAideffectService.insert(Entity);
		    resultJson.setSuccess(ResultJson.SUCCESS_YES);
		    resultJson.setMessage(ResultJson.MESSAGE);
	    } catch (BusinessException e) {
	    	resultJson.setSuccess(ResultJson.SUCCESS_NO);
		    resultJson.setMessage(e.getMessage());
		    resultJson.setResults(null);
	    } catch (Exception e1){
	    	resultJson.setSuccess(ResultJson.SUCCESS_NO);
		    resultJson.setMessage("保存失败");
		    resultJson.setResults(null);
		    e1.printStackTrace();
	    } finally{
	    	try {
	    		result = mapper.writeValueAsString(resultJson);
		    }catch (IOException e) {
		    	e.printStackTrace();
		    }
		    return result;
	    }
	}
	
	/**
	 * 删除相关困难人员的指定跟踪服务记录
	 */
	@RequestMapping(value="/deleteAidEffect", method = RequestMethod.POST)
	@ResponseBody
	public String delAidEffect(@RequestParam("id") String id){
		String result = "";
		ResultJson resultJson = new ResultJson(); 
		try {
			knryAideffectService.deleteByPrimaryKey(id);
			resultJson.setSuccess(ResultJson.SUCCESS_YES);
			resultJson.setMessage(ResultJson.MESSAGE);
		} catch (Exception e) {
			resultJson.setSuccess(ResultJson.SUCCESS_NO);
			resultJson.setMessage("删除失败");
			e.printStackTrace();
		}finally{
			try {
				result = mapper.writeValueAsString(resultJson);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	
	/**
	 * 回显困难人员指定就业援助效果台账记录
	 * @return
	 */
	@RequestMapping(value="/getAidEffectInfo", method = RequestMethod.POST)
	@ResponseBody
	public KnryAideffectData getAidEffectInfo(@RequestParam("id") String Id){
		KnryAideffectData knryAideffectData = new KnryAideffectData();
		KnryAideffectEntity knryAideffectEntity = knryAideffectService.selectByPrimaryKey(Id);
		knryAideffectData.toData(knryAideffectData, knryAideffectEntity);
		return knryAideffectData;
	}
	
	/**
	 * 修改保存困难人员就业援助效果服务台账
	 * @return
	 */
	@RequestMapping(value="/updateAidEffect", method = RequestMethod.POST)
	@ResponseBody
	public String updateAidEffectInfo(KnryAideffectData record){
		String result = "";
		ResultJson resultJson = new ResultJson();
		KnryAideffectEntity knryAideffectEntity = new KnryAideffectEntity();
		try {
			//将entity转化为实体类dto
			knryAideffectEntity.toEntityData(knryAideffectEntity, record, "");
			knryAideffectService.updateByPrimaryKey(knryAideffectEntity);
			resultJson.setSuccess(ResultJson.SUCCESS_YES);
			resultJson.setMessage(ResultJson.MESSAGE);
		} catch (Exception e) {
			resultJson.setSuccess(ResultJson.SUCCESS_NO);
			resultJson.setMessage("修改失败");
			e.printStackTrace();
		}finally{
			try {
				result = mapper.writeValueAsString(resultJson);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	
}
